#import "ProtocolHandler.h"

@implementation ProtocolHandler

+ (BOOL)canInitWithRequest:(NSURLRequest*)request {
  if ([[[request URL] absoluteString] isEqualToString:@"http://localhost:0/check"]) {
    return YES;
  }
  return NO;
}

+ (NSURLRequest*)canonicalRequestForRequest:(NSURLRequest*)request {
  return request;
}

- (id)initWithRequest:(NSURLRequest*)request
       cachedResponse:(NSCachedURLResponse*)cachedResponse
               client:(id<NSURLProtocolClient>)client {
  if ((self = [super initWithRequest:request
                      cachedResponse:cachedResponse
                              client:client])) {
  }
  return self;
}

- (void)startLoading {
  NSMutableDictionary* headerFields = [NSMutableDictionary dictionary];
  // This request is done by an AJAX call, cross origin must be allowed.
  [headerFields setObject:@"*" forKey:@"Access-Control-Allow-Origin"];
  NSHTTPURLResponse* response = [[NSHTTPURLResponse alloc]
                                 initWithURL:[NSURL URLWithString:@"http://localhost:0/check"]
                                                     statusCode:200
                                                     HTTPVersion:@"HTTP/1.1"
                                                     headerFields:headerFields];
  [self.client URLProtocol:self
        didReceiveResponse:response
        cacheStoragePolicy:NSURLCacheStorageNotAllowed];
  [self.client URLProtocol:self didLoadData:[NSData data]];
  [self.client URLProtocolDidFinishLoading:self];
  [response release];
}

- (void)stopLoading {
  // Nothing to do.                                                              
}



@end
